package com.sinosoft.jwjw.component.controller.system;

import com.sinosoft.jwjw.base.BaseCrudController;
import com.sinosoft.jwjw.base.BaseCrudService;
import com.sinosoft.jwjw.component.model.system.SDemo;
import com.sinosoft.jwjw.component.service.system.SDemoService;
import com.sinosoft.jwjw.utils.ConvertUtil;
import com.sinosoft.jwjw.utils.ResultDetail;
import com.sinosoft.jwjw.utils.ResultPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;

/**
 * @author: ericliu
 * @date: 2019/2/16
 * @description:
 */

@Controller
@RequestMapping(value = "/sDemo")
public class SDemoController extends BaseCrudController<SDemo,String> {

    @Autowired
    SDemoService sDemoService;

    @Override
    public BaseCrudService<SDemo, String> getBaseService() {
        return sDemoService;
    }

    /**
     * 测试是否成功启动
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/")
    public String index() {
        return "ok";
    }

    //创建25条数据  临时测试生成数据使用
    @ResponseBody
    @RequestMapping(value = "/createdata")
    public String createdata(HttpServletRequest request) {

        ResultDetail result = new ResultDetail();

        for (int i = 0; i < 25; i++) {
            SDemo sDemo = new SDemo();
            String newi = String.format("%03d", i);
            sDemo.setId("Id" + newi);
            sDemo.setChineseName("ChineseName" + newi);
            sDemo.setLoginName("LoginName" + newi);
            sDemo.setLoginPwd("LoginPwd" + newi);
            sDemo.setCreator("Creator" + newi);
            sDemo.setCreateTime(ConvertUtil.toSqlDate(new Date()));
            sDemo.setEditor("Editor" + newi);
            sDemo.setEditTime(ConvertUtil.toSqlDate(new Date()));
            sDemo.setIsState(1);
            sDemo.setIsDel(0);
            result = sDemoService.inserts(sDemo);
        }
        return "finished";
    }


    @ResponseBody
    @RequestMapping(value = "/save" , method = RequestMethod.POST)
    public String save(HttpServletRequest request) {

        //获取参数
        RequestParamUtils();
        ResultDetail result = new ResultDetail();

        //解析处理参数
        String id = ConvertUtil.toString(params.get("id"));//


        try {
            if ("".equals(id)) {
                // 新增
                id = UUID.randomUUID().toString().replace("-", "").toLowerCase();
                SDemo sDemo = new SDemo();

                sDemo = ConvertUtil.toObject(params.toString() , SDemo.class);
                sDemo.setId(id);

                // 默认值
                sDemo.setCreator("从session获取到用户id");
                sDemo.setCreateTime(ConvertUtil.toSqlDate(new Date()));
                sDemo.setEditor("从session获取到用户id");
                sDemo.setEditTime(ConvertUtil.toSqlDate(new Date()));
                sDemo.setIsState(1);
                sDemo.setIsDel(0);

                result = sDemoService.inserts(sDemo);
            } else {
                // 编辑
                // 默认值
                params.put("editTime", ConvertUtil.toDateString(new Date()));
                params.put("editor", "UserIDByCash");
                result = sDemoService.updateBaseInfoById(params);
            }
        } catch (Exception ee) {
            result.setCode(500);
            result.setMsg("保存失败");
            result.setInnermsg(ee.toString());
        } finally {
            return result.toString();
        }
    }

    @ResponseBody
    @RequestMapping(value = "/querylists" , method = RequestMethod.POST)
    public String querylist(HttpServletRequest request) {

        //获取参数
        RequestParamUtils();
        ResultPage resultPage = new ResultPage();

        //解析处理参数
        String visitStartTime = ConvertUtil.toString(params.get("visitStartTime"));//yyyy-MM-dd HH:mm:ss
        String visitEndTime = ConvertUtil.toString(params.get("visitEndTime"));//yyyy-MM-dd HH:mm:ss
        String testText = ConvertUtil.toString(params.get("testText"));

        resultPage = sDemoService.getListPage(pageIndex, pageSize, testText);

        return resultPage.toString();
    }
}
